<?php
include("Config.php");
include("Mysql_class.php");
include("Define.php");
include 'Page_class.php';
include_once str_replace( '\\' , '/' , realpath(dirname(__FILE__).'/../')).'/compile/user/index_template.php';
$tpl = new Template(array('debug' => true));

//获取系统设置
if(!isset($results)){
	$db = new db($conf);
	$fields = array("*");
	$results = $db->field($fields)->select("sc_system");
	$results = $results?$results[0]:array("webstate"=>0);
}
if($results['webstate']==0){
	echo "<meta charset='utf-8' />".$results['content'];exit;
}

//获取系统设置
function set($field=array("*")){
	global $conf;
	$db = new db($conf);
	$info = $db->field($field)->where('Id=1')->select("sc_set");
	return $info[0];
}

function page1($num,$psize,$page,$url=''){
    global $conf;
    $db = new db($conf);
    if($url){        
        $pagelist = new page($num,$psize,$page,$url."&page={page}",2);
    }else{
        $pagelist = new page($num,$psize,$page,$_SERVER['PHP_SELF']."?page={page}",2);
    }  
    return $pagelist->myde_write();
}

function getczinfo($id){
    global $conf;
	$db = new db($conf);
	$info = $db->field(['*'])->where("Id=$id and uid=".$_SESSION['userid'])->select("sc_cashcz");
	if($info){
	    return $info[0];
	}else{
	    return ['Id'=>0,'uppic'=>'','cash'=>''];
	}
}


function czlist($page=1,$psize=10,$uid=0){
	global $conf;
	$db = new db($conf);	
	$result = $db->field(array("*"))->where("uid=$uid")->order("Id desc")->limit($page,$psize)->select("sc_cashcz");		
	return $result;
}

function czlistnum($uid){
	global $conf;
	$db = new db($conf);	
	$result = $db->field(array("count(*)"))->where("uid=$uid")->select("sc_cashcz");
	return $result[0]['count(*)'];
}

function menuinfo($cid){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->where("Id=$cid")->select("sc_menu");
	if($info){
		return $info[0];
	}else{
		return false;
	}
}

function menulist(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(['*'])->where("menu=1")->order("Id asc")->select("sc_menu");
	return $list;
}

function hxlist($page,$psize){
	global $conf;
	$db = new db($conf);	
	$where = "hxid=".$_SESSION['userid'];
	
	$list = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_orders");
	for($i=0;$i<count($list);$i++){
		$list[$i]['glist'] = json_decode($list[$i]['goods'],true);
		$list[$i]['hxtime'] = '';
		if($list[$i]['hxid']>0){
			$hxinfo = $db->field(['*'])->where("oid=".$list[$i]['Id'])->order("Id desc")->limit("0,1")->select("sc_hxrecord");
			if($hxinfo){
				$list[$i]['hxtime'] = $hxinfo[0]['addtime'];
			}
		}
	}
	return $list;
}


function hxinfonum($id,$uid){
	global $conf;
	$db = new db($conf);
	$where = "uid=$uid";	
	if($id>0){
		$where .= " and oid=$id";
	}
	$list = $db->field(['count(*)'])->where($where)->select("sc_hxrecord");
	return $list[0]['count(*)'];
}

function hxinfo($page,$psize,$id,$uid){
	global $conf;
	$db = new db($conf);
	$where = "h.uid=$uid";
	if($id>0){
		$where .= " and h.oid=$id";
	}	
		
	$list = $db->field(['h.*','m.tname as hxname'])->where($where)->order("h.Id desc")->limit($page,$psize)->select("sc_hxrecord h left join sc_member m on m.Id=h.hxid");
	for($i=0;$i<count($list);$i++){
		$list[$i]['gname'] = '';
		$oinfo = $db->field(['*'])->where("Id=".$list[$i]['oid'])->select("sc_orders");
		if($oinfo){
			$glist = json_decode($oinfo[0]['goods'],true);
			for($n=0;$n<count($glist);$n++){
				$list[$i]['gname'] .= "【".$glist[$n]['name']."】";
			}			
		}
	}
	return $list;
}

function hxlistnum(){
	global $conf;
	$db = new db($conf);
	$where = "hxid=".$_SESSION['userid'];
	
	$list = $db->field(array("count(*)"))->where($where)->select("sc_orders");
	return $list[0]['count(*)'];
}

function orderinfo($ocode){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->where("hxcode='".$ocode."' and cplx<3")->order("Id desc")->limit("0,1")->select("sc_orders");	
	if($info){
		$info[0]['glist'] = json_decode($info[0]['goods'],true);
		$info[0]['hxtime'] = '';
		if($info[0]['hxid']>0){
			$hxinfo = $db->field(['*'])->where("oid=".$info[0]['Id'])->order("Id desc")->limit("0,1")->select("sc_hxrecord");
			if($hxinfo){
				$info[0]['hxtime'] = $hxinfo[0]['addtime'];
			}
		}
		return $info[0];
	}else{
		return false;
	}
}

function notify($oid){
	global $conf;
	$db = new db($conf);
	$oinfo = $db->field(["*"])->where("Id=$oid and js=0")->select("sc_payorder");
	if($oinfo){
		$db->where("Id=".$oinfo[0]['Id'])->update("sc_payorder",['js'=>1]);
		$glist = json_decode($oinfo[0]['goods'],true);
		
		for($n=0;$n<count($glist);$n++){//更新库存			
			$db->where(array("Id"=>$glist[$n]['id']))->update("sc_goods",["gtotal"=>"(gtotal - ".$glist[$n]['num'].")","selltotal"=>"(selltotal + ".$glist[$n]['num'].")"]);
		}
		$info = $db->field(["*"])->where("Id=".$oinfo[0]['uid'])->select("sc_member");
		if($info){
			//清除购物车
			$dresult = $db->where(array("uid"=>$info[0]['Id']))->delete("sc_buycart");
			$set = set();
			$cash = $oinfo[0]['cash'];
			$alllj = $oinfo[0]['alllj'];
			$pay = $oinfo[0]['pay'];
			$type = $oinfo[0]['type'];			
			$cplx = $oinfo[0]['cplx'];			
			$pro = $oinfo[0]['pro'];
			$cit = $oinfo[0]['cit'];
			$cou = $oinfo[0]['cou'];
			$gtype = $oinfo[0]['gtype'];

			//消费记录	
			if($cash>0){
			    if($pay==4){
			        $db->insert("sc_spend",array("uid"=>$info[0]['Id'],"cash"=>$cash,"type"=>11,"pay"=>$pay,"addtime"=>date("Y-m-d H:i:s")));
			    }else{
			        $db->insert("sc_spend",array("uid"=>$info[0]['Id'],"cash"=>$cash,"type"=>1,"pay"=>$pay,"addtime"=>date("Y-m-d H:i:s")));
			    }
										
			}
			$oinfo[0]['js'] = 1;
			if($pay<4){
				$oinfo[0]['state']=2;
			}
						
			unset($oinfo[0]['Id']);
			if($type==1){
				do{
					$oinfo[0]['hxcode'] = getRandom(8);
					$exit = $db->field(['*'])->where("hxcode='".$oinfo[0]['hxcode']."'")->select("sc_orders");					
				}while($exit);
			}
			$db->insert("sc_orders",$oinfo[0]);
			$oid = $db->done("LAST_INSERT_ID()");
			$oid = $oid[0]['LAST_INSERT_ID()'];
			$jiedian = explode(",",$info[0]['Id'].",".$info[0]['jiedian']);
			
			$year = date("Y");
			$month = date("m");
			$day = date("d");	
			$yjcash = $alllj;			
			if($cplx==2){
				if($pay<4){
					$tjinfo = $db->field(array("*"))->where("Id=".$info[0]['tjid']." and level>0")->select("sc_member");
					if($info[0]['level']==0){
						$db->where("Id=".$info[0]['Id'])->update("sc_member",array("level"=>1));							
						$db->insert("sc_uplevel",['uid'=>$info[0]['Id'],'levela'=>$info[0]['level'],'levelb'=>1,'addtime'=>date("Y-m-d H:i:s"),'state'=>1,'type'=>1,'content'=>'购买产品赠送']);
						$lbid = $info[0]['lbid'];		
						if($tjinfo){
							if($tjinfo[0]['level']>1){
								$lbid = $tjinfo[0]['Id'];
								$pxnum = 0;
							}else{
								$lbid = $tjinfo[0]['lbid'];
								$pxnum = 1;
							}												
							
							if($tjinfo[0]['level']==1){
								$ztnum = $db->field(['count(*)'])->where('level>0 and state=1 and tjid='.$tjinfo[0]['Id'])->select("sc_member");
								if($ztnum[0]['count(*)']>1){
									$db->where("Id=".$tjinfo[0]['Id'])->update("sc_member",array("level"=>2));													
									$db->insert("sc_uplevel",array("uid"=>$tjinfo[0]['Id'],"levela"=>1,"levelb"=>2,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"lx"=>1,"content"=>"直推会员升级","state"=>1));	
								}																						
							}																	
						}
						$db->where("Id=".$info[0]['Id'])->update("sc_member",array("lbid"=>$lbid,"pxnum"=>$pxnum));
						$ztnum = $db->field(['count(*)'])->where('level>0 and state=1 and tjid='.$info[0]['Id'])->select("sc_member");
						if($ztnum[0]['count(*)']>1){
							$db->where("Id=".$info[0]['Id'])->update("sc_member",array("level"=>2));													
							$db->insert("sc_uplevel",array("uid"=>$info[0]['Id'],"levela"=>1,"levelb"=>2,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"lx"=>1,"content"=>"直推会员升级","state"=>1));	
						}
						$db->where("Id in(".$info[0]['jiedian'].")")->update("sc_member",['onum'=>"(onum + 1)"]);
					}else{
						$lbid = $info[0]['lbid'];		
					}
									
					if($tjinfo && $set['ztcash']>0 && $tjinfo[0]['state']==1){					
						$ztcash = round($set['ztcash']*$yjcash/100,2);					
						if($ztcash>0){
							$db->where("Id=".$tjinfo[0]['Id'])->update("sc_member",array("jjcash"=>"(jjcash + $ztcash)","jjcash1"=>"(jjcash1 + $ztcash)"));
							$db->insert("sc_cash",array("uid"=>$tjinfo[0]['Id'],"cash"=>$ztcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>1,"adda"=>1,'oid'=>$oid));
							if($set['pjcash']>0 && $tjinfo[0]['tjid']>0 && $tjinfo[0]['level']==2){
								$pjcash = round($ztcash*$set['pjcash']/100,2);
								if($pjcash>0){
									$pjinfo = $db->field(['*'])->where("Id=".$tjinfo[0]['tjid']." and state=1 and level=2")->select("sc_member");
									if($pjinfo){
										$db->where("Id=".$pjinfo[0]['Id'])->update("sc_member",array("jjcash"=>"(jjcash + $pjcash)","jjcash1"=>"(jjcash1 + $pjcash)"));
										$db->insert("sc_cash",array("uid"=>$pjinfo[0]['Id'],"cash"=>$pjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>3,"adda"=>1,'oid'=>$oid));
									}
								}
							}
						}														
					}
					
					if($lbid>0 && $set['jdcash']>0){
						$lbinfo = $db->field(array("*"))->where("Id=$lbid and state=1")->select("sc_member");
						if($lbinfo){
							$jjcash = round($yjcash*$set['jdcash']/100,2);						
							if($jjcash>0){
								$db->where("Id=".$lbinfo[0]['Id'])->update("sc_member",array("jjcash"=>"(jjcash + $jjcash)","jjcash1"=>"(jjcash1 + $jjcash)"));
								$db->insert("sc_cash",array("uid"=>$lbinfo[0]['Id'],"cash"=>$jjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"adda"=>1,'oid'=>$oid));
								if($set['pjcash']>0 && $lbinfo[0]['tjid']>0){
									$pjcash = round($jjcash*$set['pjcash']/100,2);
									if($pjcash>0){
										$pjinfo = $db->field(['*'])->where("Id=".$lbinfo[0]['tjid']." and state=1 and level=2")->select("sc_member");
										if($pjinfo){
											$db->where("Id=".$pjinfo[0]['Id'])->update("sc_member",array("jjcash"=>"(jjcash + $pjcash)","jjcash1"=>"(jjcash1 + $pjcash)"));
											$db->insert("sc_cash",array("uid"=>$pjinfo[0]['Id'],"cash"=>$pjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>3,"adda"=>1,'oid'=>$oid));
										}
									}
								}
							}																														
						}
					}
					
					$dl3 = false;
					$dl2 = false;
					if($set['dlcash3']>0){
						$dlinfo = $db->field(["*"])->where("pro=$pro and cit=$cit and cou=$cou and dllevel=3 and state=1")->select("sc_agent");
						if($dlinfo){
							$jjcash = round($set['dlcash3']*$yjcash/100,2);
							$db->where("Id=".$dlinfo[0]['uid'])->update("sc_member",['jjcash'=>"(jjcash + $jjcash)","jjcash1"=>"(jjcash1 + $jjcash)"]);
							$db->insert("sc_cash",array("uid"=>$dlinfo[0]['uid'],"cash"=>$jjcash,'oid'=>$oid,"addtime"=>date("Y-m-d H:i:s"),"type"=>9,"adda"=>1));	
							$dl3 = true;
						}
					}
					if($set['dlcash2']>0){
						$dlinfo = $db->field(["*"])->where("pro=$pro and cit=$cit and dllevel=2 and state=1")->select("sc_agent");
						if($dlinfo){
							if($dl3){
								$jjcash = round(($set['dlcash2'] - $set['dlcash3'])*$yjcash/100,2);
							}else{
								$jjcash = round($set['dlcash2']*$yjcash/100,2);
							}
							if($jjcash>0){
								$db->where("Id=".$dlinfo[0]['uid'])->update("sc_member",['jjcash'=>"(jjcash + $jjcash)","jjcash1"=>"(jjcash1 + $jjcash)"]);
								$db->insert("sc_cash",array("uid"=>$dlinfo[0]['uid'],"cash"=>$jjcash,'oid'=>$oid,"addtime"=>date("Y-m-d H:i:s"),"type"=>9,"adda"=>1));
								$dl2 = true;
							}						
						}
					}
					if($set['dlcash1']>0){
						$dlinfo = $db->field(["*"])->where("pro=$pro and dllevel=1 and state=1")->select("sc_agent");
						if($dlinfo){
							if($dl2){
								$jjcash = round(($set['dlcash1'] - $set['dlcash2'])*$yjcash/100,2);
							}elseif($dl3){
								$jjcash = round(($set['dlcash1'] - $set['dlcash3'])*$yjcash/100,2);
							}else{
								$jjcash = round($set['dlcash1']*$yjcash/100,2);
							}
							if($jjcash>0){
								$db->where("Id=".$dlinfo[0]['uid'])->update("sc_member",['jjcash'=>"(jjcash + $jjcash)","jjcash1"=>"(jjcash1 + $jjcash)"]);
								$db->insert("sc_cash",array("uid"=>$dlinfo[0]['uid'],"cash"=>$jjcash,'oid'=>$oid,"addtime"=>date("Y-m-d H:i:s"),"type"=>9,"adda"=>1));	
							}					
						}
					}

					//团队奖励需要调整
					$tlevel = 0;
					$pjnum = 0;
					for($i=0;$i<count($jiedian)-1;$i++){
						$tdinfo = $db->field(['*'])->where("Id = ".$jiedian[$i])->select("sc_member");
						if($tdinfo){
							if($i==0){
								$db->where("Id=".$jiedian[$i])->update("sc_member",['selforder'=>"(selforder + $yjcash)"]);
							}else{						
								$db->where("Id=".$jiedian[$i])->update("sc_member",['teamorder'=>"(teamorder + $yjcash)"]);
							}						
							
							if($tdinfo[0]['tlevel']>0 && $i>0){
								if($tdinfo[0]['tlevel']>$tlevel && $tdinfo[0]['state']==1){
									if($tlevel==0){
										$jjcash = round(($set['tdjl'.$tdinfo[0]['tlevel']])*$yjcash/100,2);
									}else{										
										$jjcash = round(($set['tdjl'.$tdinfo[0]['tlevel']] - $set['tdjl'.$tlevel])*$yjcash/100,2);
									}
									if($jjcash>0){										
										$sjcash = $jjcash;																	
										$db->where("Id=".$tdinfo[0]['Id'])->update("sc_member",array("jjcash"=>"(jjcash + $sjcash)","jjcash1"=>"(jjcash1 + $sjcash)"));
										if($sjcash>0){
											$db->insert("sc_cash",array("uid"=>$tdinfo[0]['Id'],"cash"=>$sjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>5,"adda"=>1,'oid'=>$oid));
										}
									}
									$tlevel = $tdinfo[0]['tlevel'];
									$pjnum = 0;
								}elseif($tdinfo[0]['tlevel']<=$tlevel && $pjnum<=$set['ceng4']){
									if($pjnum<=$set['ceng1']){
										$bili = $set['tdpj1'];
									}elseif($pjnum<=$set['ceng2']){
										$bili = $set['tdpj2'];
									}elseif($pjnum<=$set['ceng3']){
										$bili = $set['tdpj3'];
									}else{
										$bili = $set['tdpj4'];
									}
									if($bili>0 && $tdinfo[0]['state']==1){
										$pjcash = round($jjcash*$bili/100,2);
										if($pjcash>0){
											$db->where("Id=".$tdinfo[0]['Id'])->update("sc_member",array("jjcash"=>"(jjcash + $pjcash)","jjcash1"=>"(jjcash1 + $pjcash)"));											
											$db->insert("sc_cash",array("uid"=>$tdinfo[0]['Id'],"cash"=>$pjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>6,"adda"=>1,'oid'=>$oid));																														
										}
									}							
								}
								$pjnum++;
							}
							$tdlevel = $tdinfo[0]['tlevel'];
							if($tdinfo[0]['tlevel']<7 && $tdinfo[0]['level']==2){							
								for($n=$tdlevel;$n<7;$n++){
									if($n==0){
										$tjlist = $db->field(['*'])->where('level=2 and state=1 and tjid='.$tdinfo[0]['Id'])->order('onum desc')->select("sc_member");
										if(count($tjlist)>=$set['td11'] && (($set['td12']>0 && $tjlist[$set['td12']-1]['onum']>=$set['td13']) || $set['td12']==0 || $set['td13']==0)){
											$tdlevel = 1;
										}else{
											break;
										}
									}elseif($n<3){
										$tjlist = $db->field(['count(*)'])->where("tlevel>=$n and state=1 and tjid=".$tdinfo[0]['Id'])->select("sc_member");
										if($tjlist[0]['count(*)']>=$set['td'.($n+1)]){
											$tdlevel = $n + 1;
										}else{
											break;
										}
									}else{
										$tjlist = $db->field(['count(*)'])->where("tdlevel>=$n and state=1 and tjid=".$tdinfo[0]['Id'])->select("sc_member");
										if($tjlist[0]['count(*)']>=$set['td'.($n+1)]){
											$tdlevel = $n + 1;
										}else{
											break;
										}
									}
								}														
								if($tdlevel>$tdinfo[0]['tlevel']){
									$db->where("Id=".$tdinfo[0]['Id'])->update("sc_member",['tlevel'=>$tdlevel]);
									$db->insert("sc_uplevel",array("uid"=>$tdinfo[0]['Id'],"levela"=>$tdinfo[0]['tlevel'],"levelb"=>$tdlevel,"addtime"=>date("Y-m-d H:i:s"),"type"=>2,"content"=>"业绩达标","lx"=>2,"state"=>1));
									$db->where("Id in(".$tdinfo[0]['Id'].",".$tdinfo[0]['jiedian'].") and tdlevel<$tdlevel")->update("sc_member",['tdlevel'=>$tdlevel]);
								}
							}

						}
											
					}
				}else{
					if($oinfo[0]['oid']>0){
						$db->where("Id=".$oinfo[0]['oid'])->update("sc_orders",['oid'=>$oid]);
					}
				}
				
			}elseif($cplx==3){
				if($set['tjcash1']>0){
					$tjinfo = $db->field(array("*"))->where("Id=".$info[0]['tjid']." and level>0 and state=1")->select("sc_member");
					if($tjinfo){
						$jjcash = round($yjcash*$set['tjcash1']/100,2);
						if($jjcash>0){
							$db->where("Id=".$tjinfo[0]['Id'])->update("sc_member",["jjcash"=>"(jjcash + $jjcash)","jjcash1"=>"(jjcash1 + $jjcash)"]);
							$db->insert("sc_cash",array("uid"=>$tjinfo[0]['Id'],"cash"=>$jjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>4,"adda"=>1,'oid'=>$oid));	
						}
					}
				}				
			}
			
			$yeji = $db->field(array("*"))->where("year=$year and month=$month and day=$day")->select("sc_yeji");
			if($yeji){
				$yjfield['cash'.$cplx] = "(cash$cplx + $cash)";
				if($cplx==2 && $pay<4){
					$yjfield['yjcash'] = "(yjcash + $yjcash)";
				}				
				if($pay==1 || $pay==4){
					$yjfield["yue$cplx"] = "(yue$cplx + $cash)";
				}elseif($pay==2){
					$yjfield["weixin$cplx"] = "(weixin$cplx + $cash)";
				}else{
					$yjfield["zhifubao$cplx"] = "(zhifubao$cplx + $cash)";
				}							
				$db->where("Id=".$yeji[0]['Id'])->update("sc_yeji",$yjfield);
			}else{
				$yjfield = ["year"=>$year,"month"=>$month,"day"=>$day,"cash$cplx"=>$cash,"addtime"=>date("Y-m-d H:i:s")];
				if($pay==1 || $pay==4){
					$yjfield["yue$cplx"] = $cash;
				}elseif($pay==2){
					$yjfield["weixin$cplx"] = $cash;
				}else{
					$yjfield["zhifubao$cplx"] = $cash;
				}
				if($cplx==2 && $pay<4){
					$yjfield["yjcash"] = $yjcash;
				}				
				$db->insert("sc_yeji",$yjfield);
			}
		}
	}
}

function bankrecord($page,$psize,$uid){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("uid=".$uid)->order("Id desc")->limit($page,$psize)->select("sc_bank_record");
	return $list;
}

function bankrecordnum($uid){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("count(*)"))->where("uid=".$uid)->select("sc_bank_record");
	return $list[0]['count(*)'];
}

function bank_list(){
	global $conf;
	$db = new db($conf);
	$r = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->select("sc_bank");
	for($i=0;$i<count($r);$i++){
		$r[$i]['shortname'] = substr($r[$i]['bankname'],0,3);
	}
	return $r;
}

function bank_info($id){
	global $conf;
	$db = new db($conf);
	if(!$id || $id != intval($id)){ return false;}
	$r = $db->field(array("*"))->where("Id=$id and uid=".$_SESSION['userid'])->select("sc_bank");
	if($r){
		return $r[0];
	}else{
		return false;
	}
}

function bank(){
	global $conf;
	$db = new db($conf);
	$r = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->select("sc_bank");
	if($r){
		$str = "<option>请选择银行卡</option>";
		for($i=0;$i<count($r);$i++){
			if($r[$i]['type']==1){
				$str = $str."<option value=".$r[$i]['Id']." selected>".$r[$i]['bankname']."(".$r[$i]['bankcard'].") ".$r[$i]['truename']."</option>";
			}else{
				$str = $str."<option value=".$r[$i]['Id'].">".$r[$i]['bankname']."(".$r[$i]['bankcard'].") ".$r[$i]['truename']."</option>";
			}
		}
		$str = $str."<option value='0'>新增收款账户</option>";
	}else{
		$str = "<option>请选择银行卡</option>";
		$str = $str."<option value='0'>新增收款账户</option>";
	}
	return $str;
}

//提现记录
function txlist($page=1,$pagesize=10){
	global $conf;
	$db = new db($conf);	
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->limit($page,$pagesize)->order("Id desc")->select("sc_tixian");		
	return $result;
}

function txnum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_tixian");
	return $result[0]['count(*)'];
}

function ztnum($type=0,$uid){
	global $conf;
	$db = new db($conf);
	if($type==1){
		$num = $db->field(array("count(*)"))->where("tjid=$uid and state=1 and level>0")->select("sc_member");
	}else{
		$num = $db->field(array("count(*)"))->where("tjid=$uid")->select("sc_member");
	}	
	return $num[0]['count(*)']>0?$num[0]['count(*)']:0;
}

function province(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("areapid=0")->select("sc_area");
	$str = "<option value='0'>-请选择省-</option>";
	for($i=0;$i<count($result);$i++){
		$str = $str."<option value=".$result[$i]['areaid'].">".$result[$i]['areaname']."</option>";
	}
	return $str;
}

function address_info($id){
	global $conf;
	$db = new db($conf);
	$r1 = $db->field(array("*"))->where("Id=$id")->select("sc_address");
	if(!$r1){ return false;}
	$province = $city = $county = "<option value='0'>请选择</option>";
	$r2 = $db->field(array("*"))->where("areapid=0")->select("sc_area");

	for($i=0;$i<count($r2);$i++){
		if($r1[0]['province']==$r2[$i]['areaid']){
			$province = $province."<option value=".$r2[$i]['areaid']." selected >".$r2[$i]['areaname']."</option>";
			$city = $city."<option value=".$r2[$i]['areaid']." selected >".$r2[$i]['areaname']."</option>";
		}else{
			$province = $province."<option value=".$r2[$i]['areaid'].">".$r2[$i]['areaname']."</option>";
		}
	}
	if($r1[0]['province']==$r1[0]['city']){		
	}else{
		$r2 = $db->field(array("*"))->where("areapid=".$r1[0]['province'])->select("sc_area");

		for($i=0;$i<count($r2);$i++){
			if($r1[0]['city']==$r2[$i]['areaid']){
				$city = $city."<option value=".$r2[$i]['areaid']." selected >".$r2[$i]['areaname']."</option>";
			}else{
				$city = $city."<option value=".$r2[$i]['areaid'].">".$r2[$i]['areaname']."</option>";
			}
		}
	}
	$r2 = $db->field(array("*"))->where("areapid=".$r1[0]['city'])->select("sc_area");

	for($i=0;$i<count($r2);$i++){
		if($r1[0]['county']==$r2[$i]['areaid']){
			$county = $county."<option value=".$r2[$i]['areaid']." selected >".$r2[$i]['areaname']."</option>";
		}else{
			$county = $county."<option value=".$r2[$i]['areaid'].">".$r2[$i]['areaname']."</option>";
		}
	}
	$str['tname'] = $r1[0]['tname'];
	$str['phone'] = $r1[0]['phone'];
	$str['province'] = $province;
	$str['city'] = $city;
	$str['county'] = $county;
	$str['address'] = $r1[0]['address'];
	$str['type'] = isset($r1[0]['type']) ? $r1[0]['type'] : 0;
	return $str;
}

//地址列表
function address_list(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->select("sc_address");
	if($result){
		for($i=0;$i<count($result);$i++){
			$r1 = $db->field(array("*"))->where("areaid in(".$result[$i]['province'].",".$result[$i]['city'].",".$result[$i]['county'].")")->select("sc_area");
			if(count($r1)==2){
				$result[$i]['address'] = $r1[0]['areaname'].$r1[1]['areaname'].$result[$i]['address'];
			}else{
				$result[$i]['address'] = $r1[0]['areaname'].$r1[1]['areaname'].$r1[2]['areaname'].$result[$i]['address'];
			}			
		}
	}
	return $result;
}

function orderlist($page=1,$psize=10,$uid=0,$state=0,$type){
	global $conf;
	$db = new db($conf);
	$where = "uid=$uid and state<5";
	if($state>0 && $state<5){
		$where .= " and state=$state";
	}
	if($type==1){
		$where .= " and cplx<>2";
	}else{
		$where .= " and cplx=2";
	}
	$list = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_orders");
		
	for($i=0;$i<count($list);$i++){
		$list[$i]['glist'] = json_decode($list[$i]['goods'],true);	
	}
	
	return $list;
}

function orderlistnum($uid=0,$state=0,$type){
	global $conf;
	$db = new db($conf);
	$where = "uid=$uid and state<5";
	if($state>0 && $state<4){
		$where .= " and state=$state";
	}
	if($type==1){
		$where .= " and cplx<>2";
	}else{
		$where .= " and cplx=2";
	}
	$list = $db->field(array("count(*)"))->where($where)->select("sc_orders");	
	return $list[0]['count(*)'];
}

function messlist($page=1,$pagesize=10){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->limit($page,$pagesize)->order("Id desc")->select("sc_message");
	return $result;
}

function orderdetail($oid=0,$uid=0){
	global $conf;
	$db = new db($conf);
	$where = "Id=$oid and uid=$uid";
	$result = $db->field(array("*"))->where($where)->select("sc_orders");
	
	if($result){
		$order = $result[0];
		$order['glist'] = json_decode($order['goods'],true);
		return $order;
	}
	
	return false;
}

function messnum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_message");
	return $result[0]['count(*)'];
}

function cashrecordlist($page=1,$psize=10){
	global $conf;
	$db = new db($conf);	
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->order("Id desc")->limit($page,$psize)->select("sc_spend");		
	return $result;
}

function monthcash($page=1,$psize=10){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->order("Id desc")->limit($page,$psize)->select("sc_month_cash");		
	return $result;
}

function monthcashnum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_month_cash");
	return $result[0]['count(*)'];
}

function cashrecordnum(){
	global $conf;
	$db = new db($conf);	
	$result = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_spend");
	return $result[0]['count(*)'];
}


function cashlist($page=1,$psize=10){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->order("Id desc")->limit($page,$psize)->select("sc_cash");		
	return $result;
}

function cashlistnum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_cash");
	return $result[0]['count(*)'];
}
function bflist($page=1,$psize=10){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->order("Id desc")->limit($page,$psize)->select("sc_bfcash");		
	return $result;
}

function bflistnum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_bfcash");
	return $result[0]['count(*)'];
}
function hzlist($page=1,$psize=10){
	global $conf;
	$db = new db($conf);
	$uid = $_SESSION['userid'];
	$where = "(h.uid=$uid or h.rid=$uid)";
	$result = $db->field(array("h.*","m.uname","m.tname"))->where($where)->limit($page,$psize)->order("h.Id desc")->select("sc_huzhuan as h left join sc_member as m on m.Id=h.uid");
	for($i=0;$i<count($result);$i++){
		$r1 = $db->field(array("uname","tname"))->where("Id=".$result[$i]['rid'])->select("sc_member");
		if($r1){
			$result[$i]['runame'] = $r1[0]['uname'];
			$result[$i]['rtname'] = $r1[0]['tname'];
		}else{
			$result[$i]['runame'] = "";
			$result[$i]['rtname'] = "";
		}
	}
	return $result;
}

function hzlistnum(){
	global $conf;
	$db = new db($conf);
	$uid = $_SESSION['userid'];
	$where = "(uid = $uid or rid=$uid)";
	$result = $db->field(array("count(*)"))->where($where)->select("sc_huzhuan");
	return $result[0]['count(*)'];
}

function yejilist($page,$psize){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("uid=".$_SESSION['userid'])->order("Id desc")->limit($page,$psize)->select("sc_yeji");
	return $list;
}
function yejilistnum(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("count(*)"))->where("uid=".$_SESSION['userid'])->select("sc_yeji");
	return $list[0]['count(*)'];
}

function userlist($page,$psize,$uid){
	global $conf;
	$db = new db($conf);	
	$list = $db->field(array("*"))->where("tjid=$uid")->order("Id desc")->limit($page,$psize)->select("sc_member");	
	return $list;
}

function userlistnum($uid){
	global $conf;
	$db = new db($conf);	
	$result = $db->field(array("count(*)"))->where("tjid=$uid")->select("sc_member");	
	return $result[0]['count(*)'];
}

//分类信息
function get_cate_info($cid){
	global $conf;
	$db = new db($conf);
	$r = $db->field(array("*"))->where("Id=$cid")->select("sc_catogry");
	if($r){
		return $r[0];
	}else{
		return false;
	}	
}

function updateuser($uid,$data){
	global $conf;
	$db = new db($conf);
	$db->where("Id=$uid")->update("sc_member",[$data=>'']);	
}

function getuserinfo($uid,$tjid){
	global $conf;
	$db = new db($conf);
	$info = $db->field(['*'])->where("Id=$uid and tjid=$tjid")->select("sc_member");
	if($info){
		return $info[0];
	}
	return false;
}

function newsinfo($id,$uid){
	global $conf;
	$db = new db($conf);	
	$where = "Id=$id and uid=$uid";		
	$list = $db->field(array("*"))->where($where)->select("sc_news");
	if($list){
		for($i=0;$i<count($list);$i++){
			$list[$i]['pic1'] = json_decode($list[$i]['pic1'],true);
			$list[$i]['content'] = htmlspecialchars_decode($list[$i]['content']);
		}
		return $list[0];
	}
	return false;
}

function newslist($page,$psize,$cid=0,$uid){
	global $conf;
	$db = new db($conf);	
	$where = "uid=$uid";	
	if($cid){
		$where = $where." and cid=$cid";
	}
	$list = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_news");
	for($i=0;$i<count($list);$i++){
		$list[$i]['pic1'] = json_decode($list[$i]['pic1'],true);
		$list[$i]['content'] = htmlspecialchars_decode($list[$i]['content']);
	}
	return $list;
}

function newslistnum($cid=0,$uid){
	global $conf;
	$db = new db($conf);
	$where = "uid=$uid";	
	if($cid){
		$where = $where." and cid=$cid";
	}
	$list = $db->field(array("count(*)"))->where("state=1 and cid=$cid")->select("sc_news");
	return $list[0]['count(*)'];
}

function pregmatch($str,$type=''){
	if($type=='name'){
		$pattern = '/^[\x{4e00}-\x{9fa5}]{2,4}+$/u';//中文
	}elseif($type=='phone'){
		$pattern = '/^1[3-9]\d{9}$/';//手机
	}elseif($type=='email'){
		$pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';//邮箱
	}elseif($type=='idcard'){
		$pattern = '/^(^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2))((0[1-9])|([1-2][0-9])|30|31)\d{3}(\d|X)$)|(^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2))((0[1-9])|([1-2][0-9])|30|31)\d{2}(\d|X)$)|(^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2))((0[1-9])|([1-2][0-9])|30|31)\d{3}(\d|X)$)|(^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2))((0[1-9])|([1-2][0-9])|30|31)\d{2}(\d|X)$)/';//身份证
	}
	if(preg_match($pattern,$str)){
		return true;
	}
	return false;
}

function userinfo($id=0){
	global $conf;
	$db = new db($conf);
	$id = $_SESSION['userid'];
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_member");	
	if($result && $result[0]['state']==1){	  
		return $result[0];
	}else{
		//session_destroy();
		unset($_SESSION['userid']);
		unset($_SESSION['tname']);
		unset($_SESSION['uname']);
		unset($_SESSION['state']);
		unset($_SESSION['time']);
		echo "<script>window.location.href='login.php'</script>";
	}
}

//成功返回
function success($msg,$code=0){
	if($code){
		$str = array(1,$msg,$code);
	}else{
		$str = array(1,$msg);
	}	
	echo json_encode($str);
	exit;
}
//失败返回
function error($msg){
	$str = array(0,$msg);
	echo json_encode($str);
	exit;
}
//生成随机码
function getRandom($param){
    $str="0123456789";
    $key = "";
    for($i=0;$i<$param;$i++)
     {
         $key .= $str{mt_rand(0,9)}; //生成php随机数
     }
     return $key;
 }
 //生成随机码
function getRandom1($param){
    $str="abcdefghigklmnopqrstuvwxyz";
    $key = "";
    for($i=0;$i<$param;$i++)
     {
         $key .= $str{mt_rand(0,25)}; //生成php随机数
     }
     return $key;
 }
//二维数组查询
function is_in_array($value,$array){
	if(is_array($array)){
		foreach($array as $item){
			if(is_array($item)){
				$key = array_search($value,$item);
				if($key){ return $item["name"];}
				//else{ return false;}
			}
		}
	}
	return false;
	//if()
}

function r_post1($url = '', $param = '') {    
    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();
    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($ch, CURLOPT_URL,$postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}


//模拟POST提交
function r_post($url = '', $param = '') {
	if (empty($url) || empty($param)) {
		return false;
	}	
	$postUrl = $url;
	$curlPost = $param;
	$ch = curl_init();//初始化curl
	curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
	curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
	$data = curl_exec($ch);//运行curl
	curl_close($ch);
	
	return $data;
}

//普通会员权限验证
function upower(){
	if(isset($_SESSION['userid']) && isset($_SESSION['state'])){
		if($_SESSION['state']<1){
			//session_destroy();
			unset($_SESSION['userid']);
			unset($_SESSION['tname']);
			unset($_SESSION['uname']);
			unset($_SESSION['state']);
			unset($_SESSION['time']);
			if(isset($_GET['a'])){
			echo json_encode(array(2,"登录超时，请重新登录"));exit;}
			else{
			echo "<script>alert('帐号未审核或被禁用');window.location.href='/login.php';</script>";exit;}
		}
	}elseif(isset($_COOKIE['username']) && isset($_COOKIE['password'])){
		
		global $conf;
		$uname = $_COOKIE['username'];
		$upass = $_COOKIE['password'];
		$db =  new db($conf);
		$field = array("*");
		$where = array("uname"=>"'".$uname."'","sjcode"=>"'".$upass."'");
		$result = $db->field($field)
				->where($where)
				->select("sc_member");
		if($result){
			if($result[0]['uname']!=$uname || $result[0]['sjcode']!=$upass){
				setcookie('username','');
				setcookie('password','');
				echo "<script>window.location.href='/login.php';</script>";exit;
			}
			if($result[0]['state']==0){
				setcookie('username','');
				setcookie('password','');
				echo "<script>window.location.href='/login.php';</script>";exit;
			}
			$_SESSION['userid'] = $result[0]['Id'];
			$_SESSION['tname'] = $result[0]['tname'];
			$_SESSION['uname'] = $result[0]['uname'];
			$_SESSION['state'] = $result[0]['state'];	
			$_SESSION['time'] = time();
			/* $sjcode = getRandom(12);
			$db->where("Id=".$result[0]['Id'])->update("sc_member",array("sjcode"=>$sjcode));
			setcookie("username",$result[0]['uname'],time()+3600*24*365,"");
			setcookie("password",$sjcode,time()+3600*24*365,""); */
		}else{
			setcookie('username','',time()-1000,"");
			setcookie('password','',time()-1000,"");
			echo "<script>window.location.href='/login.php';</script>";exit;
		}
	}

	else{
		if(isset($_GET['a'])){
		echo json_encode(array(2,"未登录"));exit;}
		else{
		echo "<script>window.location.href='/login.php';</script>";exit;}
	}
}
?>